cmake_minimum_required(VERSION 3.16)

#获取当前目录名作为项目名
get_filename_component(CURRENT_DIR_PATH "${CMAKE_CURRENT_LIST_DIR}" ABSOLUTE)
get_filename_component(CURRENT_DIR_NAME "${CURRENT_DIR_PATH}" NAME)
project(${CURRENT_DIR_NAME})

#C++11标准
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if(MSVC)
    add_compile_options(/Zc:__cplusplus)
endif()

#启用Qt自动处理特性
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

#Qt5模块
find_package(Qt5 COMPONENTS Core Widgets Gui REQUIRED)

#资源文件
set(RCC_FILES
    ${CMAKE_CURRENT_SOURCE_DIR}/image.qrc
)

#源，头文件自动递归查找
file(GLOB_RECURSE CPP_FILES
    ${CMAKE_CURRENT_SOURCE_DIR}/controller/*/*.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/model/*/*.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/view/*/*.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/widgets/*/*.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
)

file(GLOB_RECURSE HEADER_FILES
    ${CMAKE_CURRENT_SOURCE_DIR}/controller/*/*.h
    ${CMAKE_CURRENT_SOURCE_DIR}/model/*/*.h
    ${CMAKE_CURRENT_SOURCE_DIR}/view/*/*.h
    ${CMAKE_CURRENT_SOURCE_DIR}/widgets/*/*.h
)

#设置虚拟目录结构

foreach(file ${CPP_FILES} ${HEADER_FILES})
    file(RELATIVE_PATH relPath "${CMAKE_CURRENT_SOURCE_DIR}" "${file}")
    get_filename_component(dir "${relPath}" DIRECTORY)
    string(REPLACE "/" "\\" group "${dir}")  # 转换为 Windows 风格路径
    source_group("${group}" FILES "${file}")
endforeach()

# 将 image.qrc 添加到 resources 虚拟目录中
source_group("resources" FILES ${RCC_FILES})


#构建目标
add_executable(${PROJECT_NAME} WIN32 ${HEADER_FILES} ${CPP_FILES} ${RCC_FILES})

#链接Qt库
target_link_libraries(${PROJECT_NAME}
    PRIVATE
    Qt5::Core
    Qt5::Gui
    Qt5::Widgets
)
